package com.nsu.evaluation.controller;

import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.pojo.Role;
import com.nsu.evaluation.service.impl.AdminUsersServiceImpl;
import com.nsu.evaluation.service.impl.RoleServiceImpl;
import com.nsu.evaluation.service.impl.StudentServiceImpl;
import com.nsu.evaluation.utils.MD5Util;
import com.nsu.evaluation.utils.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author EZ
 * 2018-5-1 23:30
 */
@Controller
@RequestMapping("/api/user/")
public class UserController {

    @Autowired
    private StudentController studentController;

    @Autowired
    private AdminUsersServiceImpl adminUsersServiceImpl;

    @Autowired
    private RoleServiceImpl roleServiceImpl;

    @Autowired
    private AdminUsersController adminUsersController;

    /**
     * 用户登陆接口
     * @param username
     * @param password
     * @param roleType
     * @return
     */
    @RequestMapping(value = "userLogin")
    @ResponseBody
    public ServerResponse userLogin(String username, String password, String roleType, HttpSession session){
        //判断用户是不是管理员账户
        String adminUserStr = PropertiesUtil.getProperty("adminUser");
        String evaluationUser = PropertiesUtil.getProperty("evaluationUser");
        String teacher = PropertiesUtil.getProperty("teacher");
        String studentStr = PropertiesUtil.getProperty("student");
        //判断参数是否正确
        if( username == null || password == null || roleType == null){
            return ServerResponse.createByErrorMessage("参数错误！");
        }
        password = MD5Util.MD5EncodeUtf8(password);
        if(adminUserStr.equals(roleType)){
            ServerResponse serverResponse = adminUsersController.adminUserLogin(username,password,roleType,session);
            serverResponse.setMsg("/api/adminUsers/toAdminIndex");
            return serverResponse;
        }else if(studentStr.equals(roleType)){
            ServerResponse serverResponse = studentController.studentLogin(username,password,roleType,session);
            serverResponse.setMsg("/api/student/toStudentIndex");
            return serverResponse;
        }else if(evaluationUser.equals(roleType)){
            ServerResponse serverResponse = adminUsersController.adminUserLogin(username,password,roleType,session);
            serverResponse.setMsg("/api/adminUsers/toEvaluationIndex");
            return serverResponse;
        }else if(teacher.equals(roleType)){
            ServerResponse serverResponse = adminUsersController.adminUserLogin(username,password,roleType,session);
            serverResponse.setMsg("/api/teacher/toTeacherIndex");
            return serverResponse;
        }
        return ServerResponse.createByErrorMessage("登陆失败");
    }

    /**
     * 准备登陆所需数据：角色列表
     * @return
     */
    @RequestMapping(value = "prepareToLogin")
    @ResponseBody
    public ModelAndView prepareToLogin(HttpServletRequest request){
        //获取角色列表并返回给登陆界面
        List<Role> roleList = roleServiceImpl.getRoleList().getData();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/login.jsp");
        modelAndView.addObject("roleList",roleList);
        return modelAndView;
    }

}
